﻿using System.Globalization;
using System.IdentityModel.Tokens.Jwt;
using System.Security.Claims;
using Common.Core;
using Domain.Core.ViewModel.Base;
using Domain.Infrastructure;
using Domain.Infrastructure.Auth;
using Domain.Infrastructure.HttpContextUser;
using Domain.Infrastructure.Redis;
using Domain.Infrastructure.ServiceExtensions;
using Domain.Model.Model;
using Domain.Model.ViewModel;
using Domain.Model.ViewModel.Base;
using Domain.Services;
using Domain.Services.Interface;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;

namespace WebApi.Controllers
{
    [ApiController]
    [Route("api/[controller]")]
    public class AreaController : ControllerBase
    {
        private readonly ILogger<AreaController> _logger;
        private readonly IRedisOperationRepository _redis;
        private readonly IUser _user;
        private readonly PermissionRequirement _requirement;
        private readonly IAreaService _area;

        public AreaController(ILogger<AreaController> logger, PermissionRequirement requirement, IUser user, IRedisOperationRepository redis, IAreaService area)
        {
            _logger = logger;
            _user = user; 
            _redis=redis;
            _requirement = requirement;
            _area = area;
        }

        [HttpGet]
        [Route("reidsset")]
        public async Task<string> RedisSet(string name)
        {
            _logger.LogDebug("请求 reidsset");
            await _redis.Set("rediskeytest", DateTime.Now.ToString()+" hello " + name,TimeSpan.FromSeconds(10));
            return "Ok";
        }
        [HttpGet]
        [Route("reidsget")]
        public async Task<string> RedisGet()
        {
            _logger.LogDebug("请求 reidsset");
            return await _redis.Get("rediskeytest");
        }
        [HttpGet]
        [Route("area")]
        public async Task<object> Area()
        {
            _logger.LogDebug("请求 area");
            return await _area.QueryByIdAsync(1);
        }
        /// <summary>
        /// 自定义封装返回体
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        [Route("model")]
        public async Task<object> Model()
        {
            _logger.LogDebug("请求 Model");

            var response = new BaseResponse<AreaModel>();
            response.Result = await _area.GetModel("10");
            _logger.LogInformation("请求 Model 返回数据："+ Newtonsoft.Json.JsonConvert.SerializeObject(response));
            return response;
        }
        [HttpGet]
        [Route("list")]
        public async Task<object> List()
        {
            _logger.LogDebug("请求 List");
            var response = await _area.SelfDefineGetList();

            _logger.LogDebug("请求 List 返回："+ Newtonsoft.Json.JsonConvert.SerializeObject(response));
            return response;
        }
        [HttpGet]
        [Route("list3")]
        public async Task<object> List3()
        {
            _logger.LogDebug("请求 List");
            var response = await _area.SelDefineGetTop3List();
            
            _logger.LogDebug("请求 List 返回：" + Newtonsoft.Json.JsonConvert.SerializeObject(response));
            return response;
        }
    }
}
